using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SharpAlbum.controls
{
    public partial class ThumbView : ListView
    {
        private BackgroundWorker bkgWorker = new BackgroundWorker();

        public ThumbView()
        {
            InitializeComponent();
            ImageList il = new ImageList();
            il.ImageSize = new Size(thumbnailSize, thumbnailSize);
            il.ColorDepth = ColorDepth.Depth32Bit;
            il.TransparentColor = Color.White;
            LargeImageList = il;
            bkgWorker.DoWork += new DoWorkEventHandler(bkgWorker_DoWork);
        }

        private void bkgWorker_DoWork(object sender, DoWorkEventArgs eventArgs) 
        {
            List<string> fl = (List<string>) eventArgs.Argument;
            foreach(string file in fl)
            {
                SetThumbnail(GetThumbnail(file));
            }
        }

        private delegate void SetThumbnailDelegate(Image image);
        private void SetThumbnail(Image image)
        {
            if (this.InvokeRequired)
            {
                SetThumbnailDelegate d = new SetThumbnailDelegate(SetThumbnail);
                this.Invoke(d, new object[] { image });
            }
            else
            {
                LargeImageList.Images.Add(image); 
                int index = LargeImageList.Images.Count - 1;
                Items[index - 1].ImageIndex = index;
            }
        }

        public Image GetThumbnail(string filename)
        {
            return GetThumbnail(filename, thumbnailSize, thumbnailSize, thumbBorderColor);
        }

        public Image GetThumbnail(string filename, int width, int height, Color borderColor)
        {
            Image image = System.Drawing.Image.FromFile(filename);
            Image retImage = image.GetThumbnailImage(width, height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);

            Graphics grp = Graphics.FromImage(retImage);
            
            Pen pn = new Pen(borderColor, 1);
            grp.DrawRectangle(pn, 0, 0, retImage.Width - 1, retImage.Height - 1);

            return retImage;
        }
        
        ///  <summary>
        /// Required, but not used
        /// </summary>
        /// <returns>true</returns>
        public bool ThumbnailCallback()
        {
            return true;
        }


        private int thumbnailSize = 95;
        public int ThumbnailSize
        {
            get { return thumbnailSize; }
            set { thumbnailSize = value; }
        }

        private Color thumbBorderColor = Color.Wheat;
        public Color ThumbBorderColor 
        {
            get { return thumbBorderColor; }
            set { thumbBorderColor = value; }
        }

        private string folderName = Path.GetDirectoryName(Application.ExecutablePath);
        public string FolderName
        {
            get { return folderName; }
            set { 
                if (!Directory.Exists(value))
                    throw new DirectoryNotFoundException();
                folderName = value;                 
            }
        }

        private void AddDefaultThumb()
        {
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(LargeImageList.ImageSize.Width, LargeImageList.ImageSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            Graphics grp = Graphics.FromImage(bmp);
            Brush brs = new SolidBrush(Color.White);
            Rectangle rect = new Rectangle(0, 0, bmp.Width - 1, bmp.Height - 1);
            grp.FillRectangle(brs, rect);
            Pen pn = new Pen(Color.Wheat, 1);

            grp.DrawRectangle(pn, 0, 0, bmp.Width - 1, bmp.Height - 1);
            LargeImageList.Images.Add(bmp);
        }

        public void loadItems() 
        {
            BeginUpdate();
            Items.Clear();
            LargeImageList.Images.Clear();
            AddDefaultThumb();
            
            string strFilter = "*.jpg;*.png;*.gif;*.bmp";

            List<string> fileList = new List<string>();
            string[] arrExtn = strFilter.Split(';');

            foreach(string filter in arrExtn) 
            {
                string[] strFiles = Directory.GetFiles(folderName, filter);
                fileList.AddRange(strFiles);
                foreach(string filename in strFiles)
                {
                    FileInfo info = new FileInfo(filename);
                    Items.Add(info.Name, 0).Tag = filename;
                }
            }

            fileList.Sort();
            EndUpdate();

            if (bkgWorker != null) 
            {
                bkgWorker.RunWorkerAsync(fileList);
            }
        }
    }
}
